Skip to content

add tag-based indexing with allow and deny lists - #277

Open
MichelleAntunes wants to merge 1 commit into
nextcloud:mainfrom
MichelleAntunes:main
Open

MichelleAntunes wants to merge 1 commit into
nextcloud:mainfrom
MichelleAntunes:main

Conversation

@MichelleAntunes

@MichelleAntunes MichelleAntunes commented Sep 9, 2026 •

Copy link
Copy Markdown

Fixes #278.

Why this matters

Context Chat currently indexes every document matching its mimetype whitelist, with no way to exclude anything. For organizations handling sensitive or confidential material — legal, healthcare, HR, compliance-regulated industries — there's currently no way to control which content gets sent to an LLM for embedding. Everything eligible is indexed by default, which is the wrong default for these environments: it means adopting Context Chat requires either accepting that every eligible document becomes part of the AI's knowledge base, or not adopting it at all.

This PR adds an opt-in mode in which admins decide with regular Nextcloud system tags what is indexed. The default is unchanged: existing instances keep indexing everything, and nothing changes for anyone unless an admin deliberately switches the mode.

How it works

On the Context Chat admin settings page the admin chooses the indexing mode:

  • All documents (default, existing behavior)
  • Only documents selected with tags

In tag mode the admin picks two lists of system tags, Allow tags and Deny tags:

Allow list Deny list What is indexed
set empty files that have an allow tag
empty set everything except files that have a deny tag
set set files that have an allow tag and no deny tag
  • At least one list must be filled in tag mode, and the same tag cannot be in both lists.
  • A tag on a folder covers everything inside it (the check walks up the ancestors of the file).
  • In mode "all" the tags are ignored.

Keeping the index in line with the rules

  • Tags added to or removed from files. The index follows the tag events. A removed tag is handled by a small background job, because the tag mapper emits the event while it is still rewriting the tag rows, so a tag on a parent folder cannot be seen yet.
  • Queue items are checked again when they are handed out, so an item queued before a tag change is dropped instead of being indexed.
  • Switching the mode schedules a background job: a full cleanup of what is no longer eligible (to tag mode), or a full indexing run (back to "all").
  • Changing the lists inside tag mode. Only files that carry the added or removed tag can change status, so one small job per tag looks at those files instead of reading every file again. A full run is only needed when the allow list goes from empty to filled (files without an allow tag must leave) or from filled to empty (files without tags must enter).
  • occ context_chat:cleanup-untagged runs the cleanup by hand.
  • Users are notified by a background job when the admin switches to tag mode, since search results change for them. The notice goes away when the admin switches back to "all".

Design decisions worth calling out

  • One place decides. TagEligibilityService answers "may this file be indexed?". The file and tag listeners, the queue and the jobs all ask it, so the rules exist once.
  • A broken config cannot wipe the index. The admin UI and API refuse tag mode with both lists empty. If a config like that is stored anyway, the service treats everything as eligible.
  • The completion check lives in a service. The "initial indexing completed" check moved from QueueController to IndexCompletionService so the tag cleanup can use it as well. It keeps the logic of the recent rewrite in QueueController (seeding last_enqueued_db_id, empty-queue fallback) and also waits for cleanup crawl jobs and pending queue actions.
  • The app config cache is cleared after saving, so the long-running background worker does not keep working with the old mode.
  • Saving settings. The admin screen saves a tag change at once and shows the old value again if the server refuses it, with the reason from the server.

Testing

Tested manually on a Nextcloud 35.0.2 instance (Assistant 4.1.0, Context Chat backend 5.5.0) with several users, after rebasing on current main (including #298): switching between the modes in both directions, the three allow/deny combinations, folder inheritance, changing the lists in tag mode, the user notification appearing and going away, the admin screen (including the refusal messages), the app upgrade, and a Context Chat query returning only sources that match the rules after a reindex.

🤖 AI (if applicable)

  • The content of this PR was partly or fully generated using AI

@github-actions

Copy link
Copy Markdown

Hello there,
Thank you so much for taking the time and effort to create a pull request to our Nextcloud project.

We hope that the review process is going smooth and is helpful for you. We want to ensure your pull request is reviewed to your satisfaction. If you have a moment, our community management team would very much appreciate your feedback on your experience with this PR review process.

Your feedback is valuable to us as we continuously strive to improve our community developer experience. Please take a moment to complete our short survey by clicking on the following link: https://cloud.nextcloud.com/apps/forms/s/i9Ago4EQRZ7TWxjfmeEpPkf6

Thank you for contributing to Nextcloud and we hope to hear from you soon!

(If you believe you should not receive this message, you can add yourself to the blocklist.)

$lastFileIdSeen = $fileId;
$mountFilesCount++;

$node = $this->rootFolder->getFirstNodeById($fileId);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is sadly immensely inefficient.

@kyteinsky

Copy link
Copy Markdown
Contributor

thank you for your effort in crafting this PR.
as mentioned in the issue, we would like to do a few changes to the direction in which it goes.
if you'd be up for making the changes, please let us know, or we can go ahead with closing this out for now.
the implementation here will surely be used if we do a new PR for it.

@MichelleAntunes

MichelleAntunes commented Nov 3, 2026 •

Copy link
Copy Markdown
Author

thank you for your effort in crafting this PR. as mentioned in the issue, we would like to do a few changes to the direction in which it goes. if you'd be up for making the changes, please let us know, or we can go ahead with closing this out for now. the implementation here will surely be used if we do a new PR for it.

Hi @kyteinsky, thank you for the detailed direction, and sorry for the late reply. I'm happy to rework the PR. My understanding of the plan:

  1. Admin-selectable allow/deny tag lists instead of a single tag.
  2. Filter on the DB side instead of resolving every node in PHP, for both the cleanup and fresh indexing.
  3. Skip untagged files in the file listener, including onDelete.
  4. A notification asking admins/users to tag their files.

I’d like to do all of these now. One question on point 2: a tags option in the server’s getByAncestorInStorage would be the cleanest way, but as far as I understand it would only be available from Nextcloud 35 on. Until then, would you accept a DB-side query inside the app (joining the tag mappings with the file cache), or would you prefer that I wait for the server API option? If the server gets the option later, I’m happy to switch to it.

Since the PR is already large, I can also split it into smaller ones. Let me know what you prefer.

Admins can limit what Context Chat indexes with system tags. The index
mode is either "all" (the default, unchanged) or "tag_only". In tag
mode the admin picks an allow list and a deny list of tags:

- allow list only: index what has an allow tag
- deny list only: index everything except what has a deny tag
- both: index what has an allow tag and no deny tag

A tag on a folder covers everything inside it, and the same tag cannot
be in both lists.

- TagEligibilityService decides whether a file may be indexed. The queue
  checks it again when an item is processed, so a stale item is dropped.
- Tag and file events keep the index in line: a file that is no longer
  eligible is removed, one that became eligible is queued.
- Switching the mode schedules a full cleanup (to tag mode) or a full
  indexing run (back to all). When only the lists change, one small job
  per added or removed tag looks at the files that carry that tag,
  instead of reading every file again.
- occ context_chat:cleanup-untagged runs the cleanup by hand.
- IndexCompletionService holds the "initial indexing completed" check,
  moved out of QueueController so the cleanup can use it too. It keeps
  the checks of the controller and also waits for cleanup crawl jobs and
  pending actions.
- The admin settings get the mode switch and the two tag selectors.
  Changes are saved at once and the server explains why it refuses one.
- Users get a notification, created by a background job, when the admin
  switches to tag mode. It goes away when the admin switches back.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01AL889gRk3a2UdQAufak8m9
Signed-off-by: MichelleAntunes <miichelleantunes@outlook.com>
@MichelleAntunes MichelleAntunes changed the title Add tag-based document indexing allowlist add tag-based indexing with allow and deny lists Nov 10, 2026
@MichelleAntunes

MichelleAntunes commented Nov 10, 2026 •

Copy link
Copy Markdown
Author

I reworked the PR and rebased it on current main (including #298). It is now a single commit, so the history is different from before. I also tested it on Nextcloud 35 with Assistant 4.1.0 and the Context Chat backend 5.5.0.

Done, from the plan above:

  • Allow and deny tag lists, selected by the admin (point 1).
  • Untagged files are skipped in the file listener, including onDelete (point 3).
  • Users get a notification when the admin switches to tag mode (point 4).
  • When the lists change, only the files that carry the changed tag are looked at, not every file.
  • The initial-indexing completion check is adapted to the recent rewrite in QueueController.

Not done yet:

  • Point 2 (filtering on the DB side). This is not done yet. The full cleanup when switching to tag mode still walks all files and checks the tag for each one in PHP, and the full indexing run still lists all files and drops the ones that do not match later, when the queue item is handed out. I left it on purpose, because it depends on my open question above (DB query inside the app now, or wait for the server option). I do not want to build it in a direction you do not prefer. Once you answer, I will do it for both.
  • There is no notice for admins of existing installs after an update. Happy to add one if you want it.

The PR description is updated.

@kyteinsky

Copy link
Copy Markdown
Contributor

thank you very much!
we'll review the PR again soon.
would you mind removing claude's Co-authored-by label from the commit description, and add it to "Assisted-by:"?
see for reference: https://github.com/nextcloud/.github/blob/master/AI_POLICY.md#disclosure

  • Point 2 (filtering on the DB side). This is not done yet. The full cleanup when switching to tag mode still walks all files and checks the tag for each one in PHP, and the full indexing run still lists all files and drops the ones that do not match later, when the queue item is handed out. I left it on purpose, because it depends on my open question above (DB query inside the app now, or wait for the server option). I do not want to build it in a direction you do not prefer. Once you answer, I will do it for both.

we can add this soon in the server repo and keep going that route when it's available or fallback to this method when it's not
so both are needed

  • There is no notice for admins of existing installs after an update. Happy to add one if you want it.

that's fine.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Allow-list tag to restrict which documents get indexed by Context Chat

3 participants